﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CloudDcPc.Entity;

namespace CloudDcPc.Bean
{
    public class CheckOutBean
    {
        private MySqlConnection myConn = null;
        private DBConnection dbc = null;
        private MySqlCommand cmd = null;
        public CheckOutBean()
        {
            this.dbc = new DBConnection();
            this.myConn = this.dbc.getConnection();
        }

        public CheckOut GetCheckOut(string order_num)
        {
            string sql = "select * from dc_checkout where order_num='"+order_num+"'";
            CheckOut co = null;
            try
            {
                if (myConn.State != System.Data.ConnectionState.Closed) System.Threading.Thread.Sleep(1000); myConn.Open();
                cmd = new MySqlCommand(sql,myConn);
                MySqlDataReader reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    co = new CheckOut();
                    
                        co.Checkout_num = (string)reader["checkout_num"];
                        co.Order_num = (string)reader["order_num"];
                        co.Tab_num = (int)reader["tab_num"];
                        co.Sum = (float)reader["sum"];
                        co.Final_cash = float.Parse(reader["final_cash"]+"");
                        co.Sub_sum = (float)reader["sub_sum"];
                        co.Djq_sum = (float)reader["djq_sum"];
                        co.Tgq_sum = (float)reader["tgq_sum"];
                        co.Pc_sum = (float)reader["pc_sum"];
                        co.Approver = (string)reader["approver"];
                        co.Xyk_sum = (float)reader["xyk_sum"];
                        co.Hyk_sum = (float)reader["hyk_sum"];
                        co.Time = reader["time"].ToString();
                        co.Cash_sum = (float)reader["cash_sum"];
                    
                }
            }
            finally
            {
                dbc.close();
            }
            return co;
        }

        /// <summary>
        /// 添加一条结账信息
        /// </summary>
        public bool AddCheckOut(List<CheckOut> checkOuts)
        {
            if (myConn.State != System.Data.ConnectionState.Closed) System.Threading.Thread.Sleep(1000); myConn.Open();
            MySqlTransaction myTran = myConn.BeginTransaction();
            try
            {
                for (int i = 0; i < checkOuts.Count; i++)
                {
                    string sql_in = "update dc_tab set status=1 where tab_num='" + checkOuts[i].Tab_num + "'";
                    cmd = new MySqlCommand(sql_in, myConn);
                    cmd.ExecuteNonQuery();

                    string sql = "insert into dc_checkout(checkout_num,order_num,tab_num,food_sum,drink_sum,sum,final_cash,sub_sum,cash_sum,djq_sum,xyk_sum,hyk_sum,approver,sign_name,time,tgq_sum,pc_sum) values('" + checkOuts[i].Checkout_num + "','" + checkOuts[i].Order_num + "','" + checkOuts[i].Tab_num + "','" + checkOuts[i].Food_sum + "','" + checkOuts[i].Drink_sum + "','" + checkOuts[i].Sum + "','" + checkOuts[i].Final_cash + "','" + checkOuts[i].Sub_sum + "','" + checkOuts[i].Cash_sum + "','" + checkOuts[i].Djq_sum + "','" + checkOuts[i].Xyk_sum + "','" + checkOuts[i].Hyk_sum + "','" + checkOuts[i].Approver + "','" + checkOuts[i].Sign_name + "',NOW(),'" + checkOuts[i].Tgq_sum + "','" + checkOuts[i].Pc_sum + "')";
                    cmd = new MySqlCommand(sql, myConn);
                    cmd.ExecuteNonQuery();  

                    string sql_in_1 = "update dc_order set haschecked=1 where order_num='" + checkOuts[i].Order_num + "'";
                    cmd = new MySqlCommand(sql_in_1, myConn);
                    cmd.ExecuteNonQuery();

                    string sql_cp1 = "insert into dc_hisorder (select * from dc_order where order_num='" + checkOuts[i].Order_num + "')";
                    string sql_rm1 = "delete from dc_order where order_num='" + checkOuts[i].Order_num + "'";
                    string sql_cp2 = "insert into dc_hisorders (select * from dc_orders where order_num='" + checkOuts[i].Order_num + "')";
                    string sql_rm2 = "delete from dc_orders where order_num='" + checkOuts[i].Order_num + "'";
                    string sql_cp3 = "insert into dc_hisreturned (select * from dc_returned where order_num='" + checkOuts[i].Order_num + "')";
                    string sql_rm3 = "delete from dc_returned where order_num='" + checkOuts[i].Order_num + "'";
                    cmd = new MySqlCommand(sql_cp1, myConn);
                    cmd.ExecuteNonQuery();
                    cmd = new MySqlCommand(sql_rm1, myConn);
                    cmd.ExecuteNonQuery();
                    cmd = new MySqlCommand(sql_cp2, myConn);
                    cmd.ExecuteNonQuery();
                    cmd = new MySqlCommand(sql_rm2, myConn);
                    cmd.ExecuteNonQuery();
                    cmd = new MySqlCommand(sql_cp3, myConn);
                    cmd.ExecuteNonQuery();
                    cmd = new MySqlCommand(sql_rm3, myConn);
                    cmd.ExecuteNonQuery();
                }
                myTran.Commit();
                return true;
            }
            catch (Exception ex)
            {
                myTran.Rollback();
                throw ex;
            }
            finally
            {
                dbc.close();
            }
        }
    }
}
